const app = getApp();
var request = require("../../utils/request.js");
Page({
  data: {
    taskName: null,
    tasks: null,
    historys: null,
    user: null,
    baseUrl: null
  },
  //在加载的时候 去搜索历史记录
  onLoad: function () {
    var that = this
    var url = wx.getStorageSync('baseUrl')
    var user = wx.getStorageSync("userInfo");
    that.setData({
      user: user,
      baseUrl: url
    })
    if (user != null) {
      request.get('/search/searchHistory', {
        'loginName': user.loginName
      }, function (res) {
        if (res.code === 200) {
          console.log(res.data)
          if (res.data.length > 0) {
            that.setData({
              historys: res.data
            })
          }
        }
      })
    }
  },
  
  showDetail: function (event) {
    var that = this
    var taskId = event.currentTarget.dataset.val
    //做一个判断
    if ('' != taskId) {
      wx.navigateTo({
        url: '/pages/detail/detail?taskId=' + taskId
      })
    } else {
      wx.showToast({
        title: '任务id为空！无法跳转',
        icon: 'none',
        duration: 1000
      });
    }
  },
  bindconfirm: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var taskName = e.detail.value
    if (taskName != "") {
      var task = {
        "taskName": taskName
      }
      request.get('/search/searchTasksByTaskName', task, function (res) {
        if (res.code === 200) {
          wx.hideLoading()
          if (res.data.length != 0) {
            that.setData({
              tasks: res.data
            })
          } else {
            wx.showToast({
              title: '抱歉搜索的任务信息不存在',
              icon: 'none',
              duration: 2000
            })
          }
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '查询失败',
            icon: 'error',
            duration: 1000
          })
        }
      })
    } else {
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: '请输入你要查询的内容',
        showCancel: false,
        confirmText: "知道了",
      });
    }
  },

  //点击删除历史记录
  delHistorys: function () {
    var that = this
    var user = that.data.user
    var historys = that.data.historys
    if (historys != null) {
      wx.showModal({
        title: '提示',
        content: '是否确定删除历史记录？',
        success(res) {
          if (res.confirm) {
            request.get('/search/delSearchHistory', {
              "loginName": user.loginName
            }, function (res) {
              if (res.code === 200) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1500
                })
                that.setData({
                  historys: null
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '历史记录为空不可删除',
        showCancel: false,
        confirmText: "知道了",
      });
    }
  },
  //点击搜索历史反填充
  searchTap: function (e) {
    var that = this
    var historys = that.data.historys
    var searchId = e.currentTarget.dataset.id
    var taskName = e.currentTarget.dataset.val
    if (historys != null) {
      for (var i = 0; i < historys.length; i++) {
        if (historys[i].searchId === searchId) {
          that.setData({
            taskName: taskName
          })
        }
      }
    }
  },

  // 点击取消事件
  onCancel: function () {
    wx.navigateBack({
      delta: 1
    })
  }
})